/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.gautoard.mapreduce;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.tools.mapreduce.Mapper;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;

/**
 *
 * @author dras
 */
public class TemperatureMapper extends Mapper<Entity, String, String> {

  private static final Logger log = Logger.getLogger(TemperatureMapper.class.getName());
  long cur = System.currentTimeMillis();

  public void map(Entity entity) {
    String key = (String)entity.getProperty("key"); // location of sensor
    Date dentitydate = (Date) entity.getProperty("time"); // time of reading
    long daysago = (cur - dentitydate.getTime()) / 1000 / 60 / 60 / 24;
    //only emit if Date is older than threshold
    //if (daysago > 7) {
      String sentitydate = getDateString(dentitydate);
      String value = (String) entity.getProperty("value"); // value of reading
      //Float fval = Float.parseFloat(value);
      emit(key + ":" + sentitydate, value);
      // emits the form: com.rasdesign.temperature...:20140417, 72
    //}
  }

  public synchronized String getDateString(Date d) {
    // entityday should be formatted as yyyymmdd, like 20140214 for feb 14, 2014
    DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    return formatter.format(d);
  }
}
